#include "DALEntityTranslator.h"
#include "Serialization.h"
#include "Utils.h"

DataEntity RegionToDalEntity(Region BLLEntity)
{
	DataEntity newDE;
	
	AllocateDalEntityMemory(&newDE, sizeof(int) + strlen(BLLEntity.Name) + 1);

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeString(newDE.Data, sizeof(int), BLLEntity.Name);

	return newDE;
}

Region DalEntityToRegion(DataEntity DALEntity)
{
	Region newRegion;

	newRegion.ID = DeserializeInt(DALEntity.Data, 0);
	newRegion.Name = DeserializeString(DALEntity.Data, sizeof(int));

	return newRegion;
}

DataEntity ProductToDalEntity(Product BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + sizeof(int) + strlen(BLLEntity.Name) + 1);

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeInt(newDE.Data, sizeof(int), BLLEntity.Inventory);
	SerializeString(newDE.Data, sizeof(int) + sizeof(int), BLLEntity.Name);

	return newDE;
}

Product DalEntityToProduct(DataEntity DALEntity)
{
	Product newProduct;

	newProduct.ID = DeserializeInt(DALEntity.Data, 0);
	newProduct.Inventory = DeserializeInt(DALEntity.Data, sizeof(int));
	newProduct.Name = DeserializeString(DALEntity.Data, sizeof(int) + sizeof(int));

	return newProduct;
}

DataEntity ContactToDalEntity(Contact BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + strlen(BLLEntity.Name) + 1 + strlen(BLLEntity.ContactInformation) + 1);

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeString(newDE.Data, sizeof(int), BLLEntity.ContactInformation);
	SerializeString(newDE.Data, sizeof(int) + strlen(BLLEntity.ContactInformation) + 1, BLLEntity.Name);

	return newDE;
}

Contact DalEntityToContact(DataEntity DALEntity)
{
	Contact newContact;

	newContact.ID = DeserializeInt(DALEntity.Data, 0);
	newContact.ContactInformation = DeserializeString(DALEntity.Data, sizeof(int));
	newContact.Name = DeserializeString(DALEntity.Data, sizeof(int) + strlen(newContact.ContactInformation) + 1);

	return newContact;
}

DataEntity CustomerToDalEntity(Customer BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + sizeof(int) + sizeof(int));

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeInt(newDE.Data, sizeof(int), BLLEntity.ContactID);
	SerializeInt(newDE.Data, sizeof(int) + sizeof(int), BLLEntity.RegionID);

	return newDE;
}

Customer DalEntityToCustomer(DataEntity DALEntity)
{
	Customer newCustomer;

	newCustomer.ID = DeserializeInt(DALEntity.Data, 0);
	newCustomer.ContactID = DeserializeInt(DALEntity.Data, sizeof(int));
	newCustomer.RegionID = DeserializeInt(DALEntity.Data, sizeof(int) + sizeof(int));

	return newCustomer;
}

DataEntity SupplierToDalEntity(Supplier BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + sizeof(int));

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeInt(newDE.Data, sizeof(int), BLLEntity.ContactID);

	return newDE;
}

Supplier DalEntityToSupplier(DataEntity DALEntity)
{
	Supplier newSupplier;

	newSupplier.ID = DeserializeInt(DALEntity.Data, 0);
	newSupplier.ContactID = DeserializeInt(DALEntity.Data, sizeof(int));

	return newSupplier;
}

DataEntity DeliveryToDalEntity(Delivery BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + sizeof(int) + sizeof(int));

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeInt(newDE.Data, sizeof(int), BLLEntity.ProductID);
	SerializeInt(newDE.Data, sizeof(int) + sizeof(int), BLLEntity.Amount);

	return newDE;
}

Delivery DalEntityToDelivery(DataEntity DALEntity)
{
	Delivery newDelivery;

	newDelivery.ID = DeserializeInt(DALEntity.Data, 0);
	newDelivery.ProductID = DeserializeInt(DALEntity.Data, sizeof(int));
	newDelivery.Amount = DeserializeInt(DALEntity.Data, sizeof(int) + sizeof(int));

	return newDelivery;
}

DataEntity DeliveryBatchToDalEntity(DeliveryBatch BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + sizeof(int) + sizeof(char) + (strlen(BLLEntity.DueDate) + 1) + (uintArrLen(BLLEntity.DeliveriesIDs) + 1) * sizeof(int));

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeInt(newDE.Data, sizeof(int), BLLEntity.CustomerID);
	SerializeChar(newDE.Data, sizeof(int)+sizeof(int), (char)BLLEntity.Status);
	SerializeString(newDE.Data, sizeof(int) + sizeof(int) + sizeof(char), BLLEntity.DueDate);
	SerializeUIntArray(newDE.Data, sizeof(int) + sizeof(int) + sizeof(char) + strlen(BLLEntity.DueDate) + 1, BLLEntity.DeliveriesIDs);

	return newDE;
}

DeliveryBatch DalEntityToDeliveryBatch(DataEntity DALEntity)
{
	DeliveryBatch newDeliveryBatch;

	newDeliveryBatch.ID = DeserializeInt(DALEntity.Data, 0);
	newDeliveryBatch.CustomerID = DeserializeInt(DALEntity.Data, sizeof(int));
	newDeliveryBatch.Status = (DeliveryBatchStatus)DeserializeChar(DALEntity.Data, sizeof(int) + sizeof(int));
	newDeliveryBatch.DueDate = DeserializeString(DALEntity.Data, sizeof(int) + sizeof(int) + sizeof(char));
	newDeliveryBatch.DeliveriesIDs = DeserializeUIntArray(DALEntity.Data, sizeof(int) + sizeof(int) + sizeof(char) + (strlen(newDeliveryBatch.DueDate) + 1));

	return newDeliveryBatch;
}

DataEntity SupplyToDalEntity(Supply BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + sizeof(int) + sizeof(int));

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeInt(newDE.Data, sizeof(int), BLLEntity.ProductID);
	SerializeInt(newDE.Data, sizeof(int) + sizeof(int), BLLEntity.Amount);

	return newDE;
}

Supply DalEntityToSupply(DataEntity DALEntity)
{
	Supply newSupply;

	newSupply.ID = DeserializeInt(DALEntity.Data, 0);
	newSupply.ProductID = DeserializeInt(DALEntity.Data, sizeof(int));
	newSupply.Amount = DeserializeInt(DALEntity.Data, sizeof(int) + sizeof(int));

	return newSupply;
}

DataEntity SupplyBatchToDalEntity(SupplyBatch BLLEntity)
{
	DataEntity newDE;

	AllocateDalEntityMemory(&newDE, sizeof(int) + sizeof(int) + sizeof(char) + (uintArrLen(BLLEntity.SuppliesIDs) + 1) * sizeof(int));

	SerializeInt(newDE.Data, 0, BLLEntity.ID);
	SerializeInt(newDE.Data, sizeof(int), BLLEntity.SupplierID);
	SerializeChar(newDE.Data, sizeof(int) + sizeof(int), (char)BLLEntity.Status);
	SerializeUIntArray(newDE.Data, sizeof(int) + sizeof(int) + sizeof(char), BLLEntity.SuppliesIDs);

	return newDE;
}

SupplyBatch DalEntityToSupplyBatch(DataEntity DALEntity)
{
	SupplyBatch newSupplyBatch;

	newSupplyBatch.ID = DeserializeInt(DALEntity.Data, 0);
	newSupplyBatch.SupplierID = DeserializeInt(DALEntity.Data, sizeof(int));
	newSupplyBatch.Status = (SupplyBatchStatus)DeserializeChar(DALEntity.Data, sizeof(int) + sizeof(int));
	newSupplyBatch.SuppliesIDs = DeserializeUIntArray(DALEntity.Data, sizeof(int) + sizeof(int) + sizeof(char));

	return newSupplyBatch;
}